package com.user.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.CourseRepository;
import com.achievement.entity.Course;
import com.achievement.entity.Subject;
import com.common.util.TablePage;
import com.user.dao.TeacherRepository;
import com.user.entity.Student;
import com.user.entity.Teacher;
import com.user.entity.dto.TeacherDTO;

@Service
@Transactional
public class TeacherService implements ITeacherService {
	@Autowired
	private TeacherRepository teacherDao;
	@Autowired
	private CourseRepository courseDao;

	public TeacherDTO login(String num,String password) {
		Teacher teacher = teacherDao.findLogin(num, password);
		if(teacher == null)return null;
		String department = "";
		String subject = "";
		if(teacher.getDepartment()!=null)department = teacher.getDepartment().getName();
		int i=0;
		for(Subject s:teacher.getSubjs()) {
			subject += s.getName();
			if(0!=i&&i!=teacher.getSubjs().size())subject += ",";
			i++;
		}
		return new TeacherDTO(teacher, department, subject);
	}

	@Override
	public List<Teacher> findAll() {
		// TODO Auto-generated method stub
		return (List<Teacher>) teacherDao.findAll();
	}

	@Override
	public Set<Subject> getSubjects(Long id) {
		// TODO Auto-generated method stub
		Teacher teacher = teacherDao.findOne(id);
		return teacher.getSubjs();
	}

	@Override
	public TablePage<Teacher> findPage(Specification<Teacher> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Teacher> page= teacherDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}

	@Override
	public void saveOrUpdate(Teacher teacher) {
		// TODO Auto-generated method stub
		teacherDao.save(teacher);
	}

	@Override
	public void activate(Long id) {
		// TODO Auto-generated method stub
		Teacher teacher = teacherDao.findOne(id);
		teacher.setRevoked(false);
		teacherDao.save(teacher);
	}

	@Override
	public void revoked(Long id) {
		// TODO Auto-generated method stub
		Teacher teacher = teacherDao.findOne(id);
		teacher.setRevoked(true);
		teacherDao.save(teacher);
	}

	@Override
	public boolean hasCourse(Long id) {
		if(courseDao.findByTchId(id).size()>0)
			return true;
		return false;
	}

	@Override
	public List<Teacher> findSelectingBySubj(Long id) {
		// TODO Auto-generated method stub
		return teacherDao.findSelectingBySubj(id);
	}

	@Override
	public boolean isNumExist(Teacher teacher) {
		// TODO Auto-generated method stub
		Teacher s = teacherDao.findByNum(teacher.getNum());
		if(null!=s&&teacher.getId()!=s.getId())return true;
		return false;
	}

	@Override
	public List<Long> getSubjectsId(Long id) {
		List<Long> result = new ArrayList<Long>();
		Teacher teacher = teacherDao.findOne(id);
		Set<Subject> list = new HashSet<Subject>();
		if(null!=teacher)
			list = teacher.getSubjs();
		for(Subject s:list) {
			result.add(s.getId());
		}
		return result;
	}

	@Override
	public List<Long> getCoursesId(Long id) {
		List<Long> result = new ArrayList<Long>();
		
		if(null!=id) {
			Teacher teacher = teacherDao.findOne(id);
			if(null!=teacher) {
				List<Course> list = courseDao.findByTeacherId(id);
				for(Course s:list) {
					result.add(s.getId());
				}
			}
		}
		return result;
	}

	@Override
	public List<Course> getCourses(Long id) {
		// TODO Auto-generated method stub
		return courseDao.findByTeacherId(id);
	}

	@Override
	public void updatePwd(Long id, String pwd) {
		// TODO Auto-generated method stub
		Teacher teacher = teacherDao.findOne(id);
		teacher.setPassword(pwd);
		teacherDao.save(teacher);
	}

	@Override
	public boolean isPwdTrue(Long id, String pwd) {
		// TODO Auto-generated method stub
		Teacher teacher = teacherDao.findOne(id);
		if(teacher.getPassword().equals(pwd))
			return true;
		return false;
	}

}
